<?php
App::uses('AppController', 'Controller');
/**
 * Damages Controller
 *
 * @property Damage $Damage
 */
class DamagesController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Damage->recursive = 0;
		$this->set('damages', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Damage->id = $id;
		if (!$this->Damage->exists()) {
			throw new NotFoundException(__('Invalid damage'));
		}
		$this->set('damage', $this->Damage->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Damage->create();
			if ($this->Damage->save($this->request->data)) {
				$this->Session->setFlash(__('The damage has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The damage could not be saved. Please, try again.'));
			}
		}
		$items = $this->Damage->Item->find('list');
		$units = $this->Damage->Unit->find('list');
		$users = $this->Damage->User->find('list');
		$this->set(compact('items', 'units', 'users'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Damage->id = $id;
		if (!$this->Damage->exists()) {
			throw new NotFoundException(__('Invalid damage'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Damage->save($this->request->data)) {
				$this->Session->setFlash(__('The damage has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The damage could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Damage->read(null, $id);
		}
		$items = $this->Damage->Item->find('list');
		$units = $this->Damage->Unit->find('list');
		$users = $this->Damage->User->find('list');
		$this->set(compact('items', 'units', 'users'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Damage->id = $id;
		if (!$this->Damage->exists()) {
			throw new NotFoundException(__('Invalid damage'));
		}
		if ($this->Damage->delete()) {
			$this->Session->setFlash(__('Damage deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Damage was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
